import M from "./m";

class MediaComponent extends M {
  componentName = 'media';
  media_id: string = '';
  media_url: string = '';
  meta_type: string = '';
  media_restriction: any = {};
  keyFrameImageUrl: string = '';
  // 所属账户code
  accountCode: string = '';
  "mediaName" = null;
  "mediaWidth" = 0;
  "mediaHeight" = 0;
  "mediaType" = 0;
  "mediaSignature" = '';
  "fileSize" = '';
  "videoMillisecond" = '';
  "cover_id" = "";
  max_group: number = 15;
  get length() {
    return 1;
  }

  materia_form() {
    return [{
      name: '素材',
      key: 'media',
      rules: () => {
        return this.media_restriction;
      },
      min: this.length,
      values: []
    }]
  }

  mediaMap(_function: Function): Array<any> {
    return [_function(this)]
  }
  empty() {
    this.media_id = "";
    this.media_url = "";
    this.meta_type = "";
  }

  setSrc(options: { keyFrameImageUrl?: string, media_id: string, media_url: string, meta_type: string, cover_id?: string }) {
    this.media_id = options.media_id;
    this.media_url = options.media_url;
    this.keyFrameImageUrl = options.keyFrameImageUrl || '';
    this.meta_type = options.meta_type;
    this.cover_id = options.cover_id || '';
  }
  query() {
    const type = this.meta_type;
    const valueItem = {
      [type + '_id']: "" + this.media_id,
    }
    if (type == 'image') {
      valueItem.image_url = "" + this.media_url;
    }
    if (type == 'video') {
      valueItem.cover_id = "" + this.cover_id;
    }
    return valueItem;
  }
  isEmpty() {
    return this.media_url === '';
  }
  // image.children.video_id
  // list.children.image_id
  checkMetaType(component: any) {
    const { name } = component;
    if (name === 'short_video' || name === 'video') return 'video';
    if (name === 'image') return 'image';
    return 'unknown';
  }
  constructor(options: any) {
    super(options);
    this.init(options);
    if (options.children) {
      this.meta_type = this.checkMetaType(options);
      this.tx_component = options.name;
      const children = options.children;
      const media = children.image || children.image_id || children.video_id || children.short_video1 || children.short_video2 || { restriction: {} };
      const media_restriction = media.restriction.image_restriction || media.restriction.video_restriction || {};
      this.media_restriction = media_restriction;
    }
  }
}


export default MediaComponent;
